package com.tyqx.hashchainbiz.controller.wx;

import com.tyqx.hashchainbean.module.WxSendFile;
import com.tyqx.hashchainbiz.service.WxSendFileService;
import hashchainutil.common.Dto;
import hashchainutil.common.DtoUtil;
import hashchainutil.common.EmptyUtils;
import org.apache.commons.io.FileUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;
import org.springframework.web.multipart.MultipartHttpServletRequest;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * @program: hashchain-spring
 * @description:
 * @author: Mr.Li
 * @create: 2020-10-30 21:21
 **/
@RestController
@RequestMapping("/wx/sendFile")
@CrossOrigin
public class WxSendFileController {

    @Resource
    private WxSendFileService wxSendFileService;

    @RequestMapping(value = "/sendFileToCompany")
    @ResponseBody
    public Dto addJZTAttachmentBusiness(HttpServletRequest request) throws Exception {

        try {
            String userOpenId = request.getParameter("userOpenId");
            int companyId = Integer.valueOf(request.getParameter("companyId"));
            String companyName = request.getParameter("companyName");
            String nickName = request.getParameter("nickName");
            String fileName = request.getParameter("fileName");

           /* Gson gson = new GsonBuilder()
                    .setDateFormat("yyyy-MM-dd HH:mm:ss")
                    .create();
            WxCompany wxCompany = gson.fromJson(wxCompanyInfo, WxCompany.class);*/
            MultipartHttpServletRequest multipartRequest = (MultipartHttpServletRequest) request;
            List<MultipartFile> files = multipartRequest.getFiles("sendFile");

            MultipartFile file = files.get(0);

            //获取当前项目的绝对路径
            String uploadFilePath = System.getProperty("user.dir") + "/sendFileToWxCompany";
            // 获取文件的 名称.扩展名
            String oldName = file.getOriginalFilename();
            String extensionName = "";
            // 获取原来的扩展名
            if ((oldName != null) && (oldName.length() > 0)) {
                int dot = oldName.lastIndexOf('.');
                if ((dot > -1) && (dot < (oldName.length() - 1))) {
                    extensionName = oldName.substring(dot);
                }
            }
            // 构建文件名称
            String fileName1 =   System.currentTimeMillis()+ "_" +fileName;
            // 构建文件路径
            String filePath = uploadFilePath + File.separator + fileName1;
            // 保存文件
            FileUtils.writeByteArrayToFile(new File(filePath),
                    file.getBytes());
            Date currentDate = new Date();
            SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
            Date createTime = sdf.parse(sdf.format(currentDate));
            WxSendFile wxSendFile=new WxSendFile();
            wxSendFile.setUserOpenId(userOpenId);
            wxSendFile.setCompanyId(companyId);
            wxSendFile.setFileUrl("sendFileToWxCompany"+File.separator+fileName1);
            wxSendFile.setCreateTime(createTime);
            wxSendFile.setFileName(fileName);
            int flag=wxSendFileService.insertWxSendFile(wxSendFile);
            if(flag>0){
                return DtoUtil.returnSuccess("发送学生档案成功");
            }else{
                return DtoUtil.returnSuccess("发送学生档案失败");
            }
        } catch (Exception e) {
            e.printStackTrace();
            return DtoUtil.returnFail("发生学生档案错误","100001");
        }


    }

    @RequestMapping(value = "/querySendFileByOpenId", method = RequestMethod.POST)
    @ResponseBody
    public Dto querySendFileByOpenId(String openId){
        if(EmptyUtils.isNotEmpty(openId)){
            List<WxSendFile> wxCompanyList = wxSendFileService.queryWxSendFileByUserOpenId(openId);
            if(wxCompanyList.size()>0){
                return DtoUtil.returnSuccess("查询公司成功",wxCompanyList);
            }else{
                return DtoUtil.returnSuccess("未查询到公司记录");
            }
        }else{
            return DtoUtil.returnFail("未获取到用户信息","10001");
        }

    }

    @RequestMapping(value = "/querySendFileByCompanyId", method = RequestMethod.POST)
    @ResponseBody
    public Dto querySendFileByCompanyId(String[] companyId){
        if(EmptyUtils.isNotEmpty(companyId)){
            List<WxSendFile> wxCompanyList = wxSendFileService.queryWxSendFileByCompanyId(companyId);
            if(wxCompanyList.size()>0){
                return DtoUtil.returnSuccess("查询消息成功",wxCompanyList);
            }else{
                return DtoUtil.returnSuccess("未查询到消息记录");
            }
        }else{
            return DtoUtil.returnFail("未获取到公司信息","10001");
        }

    }
}
